import csv
import xlrd


# class Ddt_data(object):
#     def my_data(name):
#         with open(name, 'r', encoding='GBK') as f:
#             reader = csv.reader(f)
#             filednames = next(reader)
#             csv_reder = csv.DictReader(f, fieldnames=filednames)
#             data1 = []
#             for row in csv_reder:
#                 d = {}
#                 for k, v in row.items():
#                     d[k] = v
#                 data1.append(d)
#             return data1


class Ddt_data(object):
    def my_data(twe, sheetname):
        # 打开excel表，填写路径
        book = xlrd.open_workbook(twe)
        # 找到sheet页
        table = book.sheet_by_name(sheetname)
        # 获取总行数总列数
        row_num = table.nrows
        col_num = table.ncols
        s = []
        key = table.row_values(0)  # 这是第一行数据，作为字典的key值
        if row_num <= 1:
            print("没数据")
        else:
            j = 1
            for i in range(row_num-1):
                d = {}
                values = table.row_values(j)
                for x in range(col_num):
                    # 把key值对应的value赋值给key，每行循环
                    d[key[x]] = values[x]
                j += 1
                # 把字典加到列表中
                s.append(d)
            return s


if __name__ == "__main__":
    print(Ddt_data.my_data('./登陆.xls', '登陆'))

